package com.suning.sawp.service.impl.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.suning.framework.dal.client.DalClient;
import com.suning.sawp.dto.mainpush.StoreManDto;
import com.suning.sawp.intf.dao.LastestChooseManDaoService;
import com.suning.sawp.po.mainpush.LastestChooseMan;

@Service("lastestChooseManDaoService")
public class LastestChooseManDaoServiceImpl implements LastestChooseManDaoService {

    private static final Logger LOGGER = LoggerFactory.getLogger(LastestChooseManDaoServiceImpl.class);
    
    @Resource
    DalClient dalClient;
    
    @Override
    public void saveLastestChooseMan(String storeCode, String categoryCode, List<StoreManDto> storeMans) {
        if (CollectionUtils.isNotEmpty(storeMans)) {
            // 插入客户端传过来的最近联系人，没有新增，已经有则更新updateTime
            List<Map<String, Object>> mapList = new ArrayList<Map<String,Object>>();
            for (StoreManDto dto : storeMans) {
                Map<String, Object> paramMap = new HashMap<String, Object>();
                paramMap.put("staffId", dto.getStaffId());
                paramMap.put("staffName", dto.getStaffName());
                paramMap.put("storeCode", storeCode);
                paramMap.put("categoryCode", categoryCode);
                paramMap.put("updateTime", new Date());
                mapList.add(paramMap);
            }
            @SuppressWarnings("unchecked")
            Map<String, Object>[] batchValues = (Map<String, Object>[]) mapList.toArray(new Map[mapList.size()]);
            try{
                dalClient.batchUpdate("LASTEST_CHOOSE_MAN.UPSERT_CHOOSE_MAN", batchValues);
            } catch (Exception e) {
                LOGGER.error("LastestChooseManDaoService.saveLastestChooseMan error", e);
            }
            
        }
    }

    @Override
    public void removeAboveChooseMan(String storeCode, String categoryCode, int limit) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        paramMap.put("categoryCode", categoryCode);
        paramMap.put("limit", limit);
        // 查找limit+1的选择人更新时间
        LastestChooseMan chooseMan = dalClient.queryForObject("LASTEST_CHOOSE_MAN.QUERY_BREAK_POINT_CHOOSE_MAN",
                paramMap, LastestChooseMan.class);
        if (null != chooseMan) {
            // 超出选择人列表上限，删除超出的选择联系人
            paramMap.put("updateTime", chooseMan.getUpdateTime());
            dalClient.execute("LASTEST_CHOOSE_MAN.REMOVE_ABOVE_CHOOSE_MAN", paramMap);
        }
    }

    @Override
    public List<LastestChooseMan> queryLastestChooseMans(String storeCode, String categoryCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        paramMap.put("categoryCode", categoryCode);
        List<LastestChooseMan> list = dalClient.queryForList("LASTEST_CHOOSE_MAN.QUERY_LASTEST_CHOOSE_MAN",
                paramMap, LastestChooseMan.class);
        
        return list;
    }

}
